  let baseURL = 'https://www.leedong.top:8081'
//let baseURL='http://127.0.0.1:3000'
// 	根据环境变量改变请求接口统一前缀
const cookie =  uni.setStorageSync('cookie', 'hello');

/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + url,
            data: params,
           
            method: "POST",
            success: (response) => {
                let res = response.data
                if (res.code === 200) {
                    resolve(res)
				
                } else {
                    reject(res)
					uni.showToast({
						title:res.message,
						icon:'none'
					})
					
                }
			
            },
           
        });
    })
}
export function post1(url, params) {

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + url,
            data: params,
           
            method: "POST",
           success: (response) => {
				resolve(response.data)
               
            
            },
           
        });
    })
}
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export  function throttle(fn, delay) {
        let valid = true;
	
        return function () {
          /* 一开始就触发---后续每隔delay时间内只触发一次 */
          if (valid) {
            valid = false; // 关闭阀门
            // 如果阀门已经打开，就继续往下
            setTimeout(() => {
              fn.apply(this, arguments); // 定时器结束后执行
              valid = true; // 执行完成后打开阀门
            }, delay);
          }
        };
}

export function geturl(url, params) {
	
    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            data: params,
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
               
            },
            method: "GET",
            success: (response) => {

                let res = response.data
                if (res.code == 200) {
                    resolve(res)
                } else {
                    reject(res)
                
                }
            },
            fail: (error) => {
                if (error && error.errmsg) {
                    reject(error.errmsg)
                    showError(error.errmsg);
                }
            }
        });
    })
}

export function get(url, params) {
	

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + url,
            data: params,
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
               
            },
            method: "GET",
            success: (response) => {
				console.log(response)
                let res = response.data
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                
                }
            },
            fail: (error) => {
                if (error && error.response) {
                    reject(error.response)
                    showError(error.response);
                }
            }
        });
    })
}

const showError = error => {
    let errorMsg = ''
    switch (error.code) {
     	case 301:
            errorMsg = '未授权，请登录'
            break
        case 400:
            errorMsg = '请求参数错误'
            break 
        case 403:
            errorMsg = '跨域拒绝访问'
            break
        case 404:
            errorMsg = '请求地址不存在'
            break
        case 500:
            errorMsg = '服务器内部错误'
            break
        case 504:
            errorMsg = '请求超时'
            break
        default:
            errorMsg = "请求失败"
            break
    }
    uni.showToast({
        title: errorMsg,
        icon: 'none',
        duration: 3000,
        complete: function() {
            setTimeout(function() {
                uni.hideToast();
            }, 3000);
        }
    });
}